0

これで少し助けが必要かもしれませんが、これを書き出すためのよりクリーンな方法があるはずです。

空ではない #endowment div の各テキスト入力を選択したい。また、「dyn_hidden」という div に、各入力に値を追加したいと書いています。そのため、ぼかしのたびにすべてを削除してから、それぞれを追加し直します。これにはもっと良い方法がありますか?

ありがとう!

jQuery("#endowment input[type='text']").blur( function() {

    // Empty out dyn_hidden div
    jQuery('#dyn_hidden').html('');

    jQuery("#endowment input[type='text']").each(function() {
        if(jQuery(this).val() !== "") { 
            end_fund      = '<input type="hidden" name="fund[]" value="'+jQuery(this).attr('title')+'">';                                                                                                                  
            end_amount    = '<input type="hidden" name="amount[]" value="'+jQuery(this).val()+'">';
            jQuery('#dyn_hidden').append(end_fund, end_amount);
        }
    });
});
4

2 に答える 2

1

この質問にあるように、このようなことができます

 $('#button').click(function() {

    var emptyTextBoxes = $('input:text').filter(function() { return this.value == ""; });
    var string = "The blank textbox ids are - \n";

    emptyTextBoxes.each(function() {
      string += "\n" + this.id;
    });
    alert(string);
  });
于 2012-10-31T19:20:16.133 に答える
1

まず、DOM が読み込まれると、すべての隠し入力を div に挿入できます。したがって、入力ぼかしが呼び出されると、その特定の値に対応する隠し入力を変更できます。

HTML 5 属性を使用して、その特定のテキスト ボックスの情報を保存できます。

このようなもの..ページでIDが重複する可能性を排除することもできます..

jQuery(function() {
    // Insert all the input corresponding to the textboxes initially
    jQuery("#endowment input[type='text']").each(function(i) { // Use i in each as iterator to store the data-attribute
        // Store the value of the textbox in the data-id attribute
        // Add the data-attribute to the textbox that stores the index
        // of corresponding textbox
        $(this).attr('data-id', i);
        var end_fund = '<input type="hidden" name="fund[]" data-param="title-' + i + '" value="' + jQuery(this).attr('title') + '">';
        var end_amount = '<input type="hidden" name="amount[]" data-param="value-' + i + '" value="">';
        jQuery('#dyn_hidden').append(end_fund, end_amount);
    });

    // Blur function of text box
    jQuery("#endowment input[type='text']").blur(function() {
        // Just change the value of corresponding input field..
        if (this.value !== "") {
            var $index = $(this).data('id');
            // Find the hidden input with data-param= "value + $index  inside
            // the #dyn_hidden  div  
            // And then set the value to it...
            $('#dyn_hidden input[data-param="value-' + $index + '"]').val(this.value);
        }
    });
});​

コード

于 2012-10-31T19:50:23.623 に答える