2012-01-04 20 views
19

मुझे विशेषता मानों को खोजने और बदलने में कठिनाई हो रही है।JQuery: विशेषता मानों को ढूंढें और बदलें

इस एचटीएमएल पर विचार करें:

<tr id="rules[0]"> 
    <td> 
     <input id="rules0.isActive1" name="rules[0].isActive" type="checkbox" value="true"/><input type="hidden" name="_rules[0].isActive" value="on"/> 
    </td> 
    <td> 
     <select id="rules0.leftCondition.name" name="rules[0].leftCondition.name"> 
      ... 
     </select> 
    </td> 

मैं उपयुक्त गिनती जैसे के साथ प्रत्येक के नाम विशेषता को अपडेट करने देख रहा हूँ

$(newRow).find('[id*="rules"]').each(function(){ 
    // Update the 'rules[0]' part of the name attribute to contain the latest count 
    this.name.replace('rules\[0\]','rules\[$count\]'); 
}); 

जहां newRow एक चर पूरे ब्लॉक पकड़े और गिनती नवीनतम काउंटर मूल्य रखती है: नियम [0] .isActive नियम [10] के लिए अद्यतन किया जाता है

मैं इस JQuery टुकड़ा का उपयोग कर रहा .isActive। हालांकि, जब मैं इस कोड को डीबग करता हूं ऐसा लगता है कि "this.name" अपरिभाषित है।

मैं यहां गलत कहां जा रहा हूं? आप .attr समारोह का उपयोग करना चाहिए तत्व की विशेषता

$(this).attr('name', $(this).attr('name').replace('rules\[0\]','rules\[$count\]')); 
+0

आप क्यों, जिसकी करते के लिए नए मूल्य निर्धारित करने और नहीं करने के लिए' name' कर सकते हैं? – jantimon

उत्तर

20

अद्यतन करने के लिए:

धन्यवाद

3

इस प्रयास करें। साथ ही, jQuery संदर्भ में इसका उपयोग करते समय आपको $ (यह) वाक्यविन्यास का उपयोग करना होगा। अगर मैं अपने इरादों को समझने तो यह ऐसा ही कुछ किया जाना चाहिए:

$(newRow).find('[id*="rules"]').each(function(){ 
    // Update the 'rules[0]' part of the name attribute to contain the latest count 
    $(this).attr('name',$(this).attr('name').replace('rules\[0\]','rules\[$count\]')); 
}); 
6

आप $(this).attr('name') बजाय this.name उपयोग करना चाहिए।

5

इस प्रयास करें: पंक्ति मान लें विशेषता नाम 'filterCol' तो आप उस विशेषता `id` के लिए

$("#trId").attr("filterCol", newValue); 
संबंधित मुद्दे