HTMLで始まり、主に2つの問題があります。 1つは、すべてのフィールドについて同じIDを持つだけでなく、いずれかのフィールドに同じフォームフィールドを使用することです。これはフィールドが1つだけであるように見えるようにする効果があります。
<input type="hidden" name="uID" id="uID" value="251|0">
<input type="hidden" name="uID" id="uID" value="252|0">
<input type="hidden" name="uID" id="uID" value="253|0">
<input type="hidden" name="uID" id="uID" value="254|0">
<input type="hidden" name="uID" id="uID" value="0|0">
あなたがそのようなルックスに、あなたのHTMLを更新する必要がありますいずれかのこの問題を解決するために:あなたが投稿した場合、最初のソリューションで
<input type="hidden" name="uID-1" id="uID-1" value="251|0">
<input type="hidden" name="uID-2" id="uID-2" value="252|0">
<input type="hidden" name="uID-3" id="uID-3" value="253|0">
<input type="hidden" name="uID-4" id="uID-4" value="254|0">
<input type="hidden" name="uID-5" id="uID-5" value="0|0">
:このようなの
<input type="hidden" name="uID[]" id="uID-1" value="251|0">
<input type="hidden" name="uID[]" id="uID-2" value="252|0">
<input type="hidden" name="uID[]" id="uID-3" value="253|0">
<input type="hidden" name="uID[]" id="uID-4" value="254|0">
<input type="hidden" name="uID[]" id="uID-5" value="0|0">
フォーム、データはあなたのすべてのフィールドの値を持つ配列になりますあなたのサーバーに行くでしょう。
私が送った2番目のソリューションでは、リクエストに複数の変数があるだけです。
は今、あなたは、次のコードを実行する必要があります値を得るために:
// Isolate the code inside a Immediately-Invoked Function Expression (IIFE)
(
function ($, window, undefined) {
// Create a variable that is going to hold all the hidden fields
var $hidden_inputs = null;
// Responsible to iterate over the elements found in your
// HTML, and returns the values in the format of val,val,val
var get_values = function get_values () {
// Register an array variable that is going
// to hold the values of all the found fields
var $string_parts = [];
// Iterate over the found fields
$hidden_inputs.each(
function() {
// And push the values of those fields into the
// values array container
$string_parts.push($(this).val());
}
);
// Finally return the array values joined with commas
return $string_parts.join(',');
}
// When the document is loaded
$(document).ready(
function() {
// Query the document for the hidden fields that works
// like that:
// Find all the input fields with
// type="hidden" that the
// name attribute contains the string uID
$hidden_inputs = $('input[type="hidden"][name*="uID"]');
// If the jQuery found more than 0 elements
if (0 < $hidden_inputs.length) {
// Get the field values and save them into the
// $values variable.
var $values = get_values();
}
}
);
}
)(jQuery, this);
また、ここで実行している例を見つけることができます:文字列の配列にそれぞれvalue
を投影するjsFiddle
は、なぜあなたは最初の場所で同じ名前の複数の '' の要素を持っているのですか?また、IDはDOM内で一意であると想定されます。あなたがあなたの答えを持っていれば@Damien @ – BenM
'$(" input [name = 'uID'] ")" map((x、elm)=> elm.value).get()。join( '、') ' – haim770
@Damienあなたが正解と賛成投票としてあなたを助けた答えをチェックしてください:)これはコミュニティが質問に答えるのを助けます:);) –