简体   繁体   English

如何使用JQuery使用单选按钮保留文本字段中的值

[英]How to retain the values in the text field with Radio buttons using JQuery

I have some code that updates form fields based on a radio button being clicked: 我有一些代码可以根据单击的单选按钮来更新表单字段:

html html

<div class="radio">
    <label><input type="radio" onclick="myFunction(this)" name="extra" value="add_extra_new">Add New Extra</label>
    <label><input type="radio" onclick="myFunction(this)" name="extra" value="exists" checked="checked">Existing Extras</label>
</div>
<span id="add_new">Name of Extra :</span>
<input id="add_more" type="text" name="nameOfExtra" >
<span id="existing">Exsisting Extras :</span>
<select id="extra-select" name="extras">
    <option>deep cleaning</option>
</select>
<span class="extra-heading">Set Price :</span>
<input id="price" type="text" name="price">

javascript javascript

var new_extra = document.getElementById("add_new");
new_extra.style.display = "none";
var extraText = document.getElementById("add_more");
extraText.style.display = "none";
var extraExist = document.getElementById("existing");
var extra_dropdown = document.getElementById("extra-select");
function myFunction(event) {
    console.log(event.value);
    if (event.value === "add_extra_new") {
        console.log(event.value);
        new_extra.style.display = "block";
        extraText.style.display="block";
        extraExist.style.display="none";
        extra_dropdown.style.display="none";
        $('input[type="radio"]').prop('checked');
        $('input[type="text"]').val("");
    } else {
        extraExist.style.display="block";
        extra_dropdown.style.display="block";
        new_extra.style.display = "none";
        extraText.style.display="none";
    }
    }   

JQuery jQuery查询

$.ajax({
    type:'POST',
    data:{ "extraOption" : extra_option}, 
    dataType : "JSON", 
    url : "login.php", 
    success:function(response){
        var data = response.data; 
        if(data){
            $.each(data, function (key, name) {    
                $('#price').val(name['price']);
                $('#hour').val(name['hours']);
                $('#minute').val(name['minutes']);
            });
        } else {        
            $('#once').val('');
            $('#reoccurence').val('');
            $('#discount').val('');
        }      
    }
});

login.php login.php

$con=mysqli_connect("localhost","root","","price");
$result2 = array();
if (isset($_POST['extraOption'])) { 
    $query=mysqli_query($con,"select * from extras where name_of_extra                                                                                     ='$_POST[extraOption]'");
    while ($row = mysqli_fetch_array($query)) {        
       $result2['data'][] = array(
           "price" => $row['price'],
           "hours" => $row['hours'],
           "minutes" => $row['minutes']
       );
    }
    echo json_encode($result2);
}

Initial state 初始状态

As you can see, form's fields have initial values. 如您所见,表单的字段具有初始值。

Now when "Add New Extra" radio button is clicked, text fields get emptied because of my code else part. 现在,当单击“添加新的额外”单选按钮时,由于我的代码的其他部分,文本字段将被清空。

After "Add New Extra" click 在“添加新的额外内容”之后,点击

My problem is: if I then click on the "Existing Extra" the text fields do not recover their initial value. 我的问题是:如果我然后单击“现有额外”,则文本字段将无法恢复其初始值。

What should I do so the text fields get their initial value back (like in the first image) at this time? 我应该怎么做才能使文本字段此时返回其初始值(如第一张图片所示)?
Thank you. 谢谢。

use this to reset to initial value 使用它重置为初始值

$('radio[value=existing_data]').click(function(){
  if($(this).is(":checked")){
    $('.setprice').val(11);//these three line set value to inputs
    $('.min').val(10);
    $('.timehrs').val(1);
 }
});

PS set select as per your need PS套根据您的需要选择

You can use localStorage , check this link https://www.w3schools.com/html/html5_webstorage.asp 您可以使用localStorage ,请检查此链接https://www.w3schools.com/html/html5_webstorage.asp

HTML 的HTML

<div class="radio">
   <label><input type="radio" onclick="myFunction(this)" name="extra" value="add_extra_new">Add New Extra</label>
   <label><input type="radio" onclick="myFunction(this)" name="extra" value="exists" checked="checked">Existing
      Extras</label>
</div>

<span id="add_new">Name of Extra :</span>
<input id="add_more" type="text" name="nameOfExtra">

<span id="existing">Exsisting Extras :</span>
<select id="extra-select" name="extras">
   <option value="">Please select</option>
   <option value="deep cleaning">deep cleaning</option>
   <option value="dishes">dishes</option>
</select>

<span class="extra-heading">Set Price :</span>
<input id="price" type="text" name="price">

JS JS

var new_extra = document.getElementById("add_new");
   new_extra.style.display = "none";
   var extraText = document.getElementById("add_more");
   extraText.style.display = "none";
   var extraExist = document.getElementById("existing");
   var extra_dropdown = document.getElementById("extra-select");

      function myFunction(event) {
        if (event.value === "add_extra_new") {
            localStorage.setItem("pirce", $('#price').val());

            new_extra.style.display = "block";
            extraText.style.display = "block";
            extraExist.style.display = "none";
           extra_dropdown.style.display = "none";
           $('input[type="radio"]').prop('checked');
           $('input[type="text"]').val("");
        } else {
           extraExist.style.display = "block";
           extra_dropdown.style.display = "block";
           new_extra.style.display = "none";
           extraText.style.display = "none";

          $('#price').val(localStorage.getItem("pirce"));
        }
     }

   $("#extra-select").on('change',function () {
      $.ajax({
         type: 'POST',
         data: {"extraOption": $(this).val()},
         dataType: "JSON",
         url: "login.php",
         success: function (response) {
            var data = response.data;
            if (data) {
               $.each(data, function (key, name) {
                  $('#price').val(name['price']);
                  $('#hour').val(name['hours']);
                  $('#minute').val(name['minutes']);
               });
            } else {
               $('#once').val('');
               $('#reoccurence').val('');
               $('#discount').val('');
            }
         }
      });
   });

Login.php Login.php

$con=mysqli_connect("localhost","root","","price");
$result2 = array();
if (isset($_POST['extraOption'])) {
   $query=mysqli_query($con,"select * from extras where name_of_extras ='$_POST[extraOption]'");
   while ($row = mysqli_fetch_array($query)) {
      $result2['data'][] = array(
         "price" => $row['price'],
         "hours" => $row['hours'],
         "minutes" => $row['minutes']
      );
   }
   echo json_encode($result2);
}

Explanation 说明

You need to check when ever user change the dropdown values. 您需要检查用户何时更改下拉值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM