javascript - 替换数组中的输入值时出现问题

标签 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

最佳答案

好的,我明白了,您想在删除行时更改输入值,所以请执行以下操作:

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();
}

关于javascript - 替换数组中的输入值时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29836000/

相关文章:

javascript - 如何在 Firefox 中跟踪和调试 JavaScript 内存泄漏?

javascript - Highcharts 日期时间本地化

html - 调整窗口大小时保持网页内容不移动

javascript - 没有 bower 的 Ember-Cli 导入 js

javascript - 为什么 Mongoose 排序比原生 javascript 排序慢?

jquery - 使用 CSS/JQuery 制作动画

jquery - 在 Onsen ui 上缓存选项卡内容

html - 具有固定位置和相对位置的重叠 div

javascript - iframe .load 事件未触发

javascript - Excel 导出出现错误 : Failed - Network Error on large exports