簡體   English   中英

如何在Enter鍵按下事件中獲取輸入值?

[英]how to get input values in enter key press event?

我試圖在不按頁面刷新的情況下按Enter提交表單。

這是我的代碼。

PHP代碼。

<form action="profile/update_profile" method="post" id="business_name_data">
 <input type="hidden" name="business_name" id="business_name"/>
</form>

update_profile函數

public function update_profile()
{
  json_encode($this->input->post('business_name'),true);
}

js代碼

 jQuery(document).keyup(function(e){

   e.preventDefault();

    if(e.which == 13){
    jQuery.ajax({
                    type : "post",
                    url :  "../profile/update_profile",
                    data : "business_name="+jQuery("#business_name").val(),
                    dataType: "json",
                    success : function(msg) 
                    {    
                        console.log(msg);
                            //jQuery("div").find(".category_view").html(msg);
                            //jQuery("#1").css("visibility","visible");
                    }
          });
             }
          });

我正在嘗試傳遞輸入值,但它會輸出null。 請幫忙。

試試這個

您必須將值設置為隱藏元素

<input type="hidden" value="busineesname" name="business_name" id="business_name"/>

jQuery(document).keyup(function(e){
    e.preventDefault();
    var code = (e.keyCode ? e.keyCode : e.which);
    if(code == 13){
    jQuery.ajax({
               type : "post",
               url :  "../profile/update_profile",
               data : {"business_name":jQuery("#business_name").val()},
               dataType: "json",
               success : function(msg) 
               {    
                        console.log(msg);
               }
             });
           }
        });

DEMO

首先要在if條件之前將值提醒為:

alert(jQuery("#business_name").val());// check this if it gives the value

並且如果它給出值並放入echo因為您沒有echoprint_r

在update_profile函數中

public function update_profile()
{
   echo json_encode($this->input->post('business_name'),true);
   //add the echo and check  

}

希望這會有所幫助!

首先,您需要在update_profile()方法中回顯值

public function update_profile()
{
  echo json_encode($this->input->post('business_name'),true);
}

下一步是檢查是否按下了Enter鍵 ,也嘗試將其從輸入字段business_name的值打印到控制台以查看其是否具有值。

jQuery(document).keyup(function (e) {
    e.preventDefault();
    if (e.keyCode == 13) {
        jQuery.ajax({
            type: "post",
            url: "../profile/update_profile",
            data: {
                "business_name": jQuery("#business_name").val()
            },
            dataType: "json",
            success: function (msg) {
                console.log(msg);
            }
        });
    }
});

如果您犯了一個錯誤並且想要一個可見的文本框,當您單擊它時,您要提交表單而不刷新頁面,您可以訂閱提交事件

  $(function(){
      $('#business_name_data').submit(function(event){
        alert('your ajax call');
        event.preventDefault();
      });
   });

http://plnkr.co/edit/tbHTjyRKEV0cxraP3R6n?p=preview

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM