根据条件单击提交按钮时更改文本字段值

时间:2018-10-30 07:11:34

标签: javascript html

嗨,我知道这必须是一个简单的问题,我是javascript新手,我想实现一下,当单击按钮提交时,根据条件,我的两个文本字段的值将以不同的方式提交,但是我不知道为什么我的文本不起作用它没有将值设置为mytext1和mytext2。我将非常感谢任何建议。谢谢

<script>

$(document).ready( function(){

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

        var date1 = document.getElementById("temp1").value;
            var user = document.getElementById("temp2").value;

            var admin = document.getElementById("temp3").value;
            if(user!='Admin') {
                document.getElementById("mytext1").value = user;
          document.getElementById("mytext2").value = date1;

      }else {

         document.getElementById("mytext1").value = admin;
          document.getElementById("mytext2").value = date1;

      }


    });
});

</script>

2 个答案:

答案 0 :(得分:0)

尝试“打开”

<script>

$(document).ready( function(){

    $('document').on('click','#submit', function() {

        var date1 = document.getElementById("temp1").value;
            var user = document.getElementById("temp2").value;

            var admin = document.getElementById("temp3").value;
            if(user!='Admin') {
                document.getElementById("mytext1").value = user;
          document.getElementById("mytext2").value = date1;

      }else {

         document.getElementById("mytext1").value = admin;
          document.getElementById("mytext2").value = date1;

      }


    });
});

</script>

答案 1 :(得分:0)

使用以下代码段,您可以区分要提交的内容。我已经使用JQuery创建了函数

请在您的问题中提供更多代码以更好地了解

$(document).on('click','#submit', function() {
  var first = $('#temp1').val();
  var second = $('#temp2').val();

  var user = $('#useradmin').val();

  if (user == 'user') {
    $('#show').html(first);
    $('#showUserAdmin').html(user);
  } else {
    $('#show').html(second);
    $('#showUserAdmin').html(user);
  }
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>

<input type="text" id="temp1" placeholder="will submit in User" value="Submit along with User"><br>
<input type="text" id="temp2" placeholder="will submit in Admin" value="Submit along with Admin"><br>
<select required id="useradmin">
  <option value="user">User</option>
  <option value="admin">Admin</option>
</select>
<button type="button" id="submit">Submit</button>

<div id="show"></div>
<div id="showUserAdmin"></div>