问题替换数组中的输入值

时间:2015-04-23 23:16:09

标签: javascript jquery html arrays prototype

我在删除行但没有工作时做了一个关于替换输入值的示例(这不是一个静态示例)。

<script src="./edit_files/prototype.js" type="text/javascript"></script>
<script src="./edit_files/application.js" type="text/javascript"></script>

<div class="contact">
<table border="0"> 
  <tr>
    <td><select class="position_id" id="obj_client_contact_attributes__position_id" name="obj_client[contact_attributes][][position_id]"><option value="1" selected="selected">INTELIGENT</option><option value="2">OTHER</option></select></td>

      <td><input class="should_change_value" id="obj_client_contact_attributes__phone_mail" name="obj_client[contact_attributes][][phone_mail]"  type="text" value="cristianoronaldo@realmadrid.com"/></td>

    <td>        
      <a href="#" onclick="mark_for_destroy_contact(this,true); return false;">DELETE</a>
        <input id="obj_client_contact_attributes__id" name="obj_client[contact_attributes][][id]" type="hidden" value="16594"/>
        <input class="should_destroy" id="obj_client_contact_attributes__should_destroy" name="obj_client[contact_attributes][][should_destroy]" type="hidden"/>
    </td>
  </tr>
</table>
</div>

<div class="contact">
<table border="0"> 
  <tr>
    <td><select class="position_id" id="obj_client_contact_attributes__position_id" name="obj_client[contact_attributes][][position_id]"><option value="1" selected="selected">INTELIGENT</option><option value="2">OTHER</option></select></td>

      <td><input class="should_change_value" id="obj_client_contact_attributes__phone_mail" name="obj_client[contact_attributes][][phone_mail]" type="text" value="ONLY THE INPUT WILL BE test@hotmail.com IF I CLICK ON DELETE"/></td>

    <td>        
      <a href="#" onclick="mark_for_destroy_contact(this,true); return false;">DELETE</a>
        <input id="obj_client_contact_attributes__id" name="obj_client[contact_attributes][][id]" type="hidden" value="16594"/>
        <input class="should_destroy" id="obj_client_contact_attributes__should_destroy" name="obj_client[contact_attributes][][should_destroy]" type="hidden"/>
    </td>
  </tr>
</table>
</div>

这是application.js文件:

function mark_for_destroy_contact(element,should_destroy,should_change_value) {
 var element_text = $(element).up('.contact').down('.position_id',0);
 element_text.className = 'position_id';
 element_text.value = '';

 if (should_destroy) {
   $(element).next('.should_destroy').value = 1;
 }

 $(element).up('.contact').hide();
}  

我尝试了这段代码,但只有删除第一行才能生效。

function mark_for_destroy_contact(element,should_destroy,should_change_value) {
  var element_text = $(element).up('.contact').down('.position_id',0);
  element_text.className = 'position_id';
  element_text.value = '';

  $('should_change_value').update("test@hotmail.com");

  if (should_destroy) {
   $(element).next('.should_destroy').value = 1;
  }
  $(element).up('.contact').hide();
}

Here is the live example in jsfiddle

Here is the example download on Github but is not replacing the input value correctly

1 个答案:

答案 0 :(得分:1)

好的,我知道了,你想在删除行时更改输入值,所以这样做:

function mark_for_destroy_contact(element,should_destroy,should_change_value) {
   var element_text = $(element).up('.contact').down('.position_id',0);
   element_text.className = 'position_id';
   element_text.value = '';

   var element_text2 = $(element).up('.contact').down('.should_change_value',0);
   element_text2.className = 'should_change_value';
   element_text2.value = 'test@hotmail.com';   

   if (should_destroy) { $(element).next('.should_destroy').value = 1;}

   $(element).up('.contact').hide();
}