【问题标题】:jquery select value in form inputjquery在表单输入中选择值
【发布时间】:2017-09-07 14:36:16
【问题描述】:

我有一张这样的桌子:

我希望当我选择 id_siswa 时它会以输入的形式退出nominal_iuran 值,但我在这方面有困难。

下图放大

我使用这种编码

<script type="text/javascript">
      jQuery(function($) {
        $('select[name=id_siswa]').on('change', function() {
          var name = $(this).find('option:selected').text();
        $('input[name=nominal_iuran]').val(name);
      });
    });
 </script> 

请大师帮帮我

【问题讨论】:

  • 那么什么不起作用?
  • 这张桌子住在哪里?
  • 我想看出来的nominal_iurannot selected
  • 我想当我选择id_siswa时它会以输入的形式退出nominal_iuran值,请帮帮我
  • “我有一个表” - 你是指 HTML &lt;table&gt; 还是数据库表?

标签: jquery forms select option


【解决方案1】:

我不确定您是如何加载数据的,但是:

如果您不想使用 ajax,那么当您首次使用数据库中的数据加载页面时,您可以向选项添加数据属性,然后将其作为值而不是名称。

jQuery(function($) {
  $('select[name=id_siswa]').on('change', function() {
    var name = $(this).find('option:selected').text();
    var otherValue = $(this).find('option:selected').attr("data-nominaliuran");
    console.log(otherValue);
    $('input[name=nominal_iuran]').val(otherValue);
    
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

If you didnt want to use ajax then when you first load the page with data from the database, you could add a data-attribute to the option and then grab that for the value instead of the name...<br>
<select name=id_siswa>
  <option value=test1 data-nominaliuran="60000">TEST1</option>
  <option value=test2 data-nominaliuran="200">TEST2</option>
  <option value=test3 data-nominaliuran="0">TEST3</option>
</select>
<br><br>NOMINAL_IURAN: <input name="nominal_iuran">

【讨论】:

    【解决方案2】:

    您不能直接使用 jQuery(或 JavaScript)查询您的表。您必须使用 PHP、Python 等服务器端脚本语言来执行此操作,然后将结果返回到您的页面。

    【讨论】:

    • 如何掌握?我还没有找到它的逻辑
    • 如果不了解您的环境,我无法回答这个问题。但如果你不会任何一种服务器端脚本语言,你应该去 [link]w3schools.com[/link] 学习 PHP 或 Python。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 2012-07-08
    • 2011-06-30
    • 2015-12-31
    • 2014-11-11
    相关资源
    最近更新 更多