簡體   English   中英

檢查輸入字段為空

[英]Checking Input Field Empty

我有一個按鈕,一旦按下將檢查數據屬性是否失敗。 如果失敗,它不會將它們發送到下一步。

問題是如果你失敗了,一旦你無法進入下一步? 有任何想法嗎

if ($('#storename').data("search") == "failed"){

   alert("Please select something.");

}else{

  //next step 

 };

像這樣的東西,抱歉沒有使用JS小提琴,但這應該清除它。 http://jsfiddle.net/gMam4/2/

替換$(".failed").attr("data-search", "failed"); with $('#storename').attr("data-search", "failed"); 在您的錨點擊事件中。 嘗試這個:

 $('#storename').on('keyup',function(e){
      if ($('#storename').attr("data-search") == "failed"){   
       alert("Please select something.");   
    }else{    
       alert("this is not failed");    
     };
 });
   $(".failed" ).click(function() {
      console.log('changing to failed');
      $('#storename').attr("data-search", "failed");
    });

DEMO

小提琴:

http://jsfiddle.net/gMam4/9/

$('#storename').on('keyup',function(e){
      if ($('#storename').data("search") === "failed"){        
       console.log("Please select something.");        
    }else{        
       console.log("this is not failed");        
     };
 });

$(".failed").on('click',function() {
     console.log('changing to failed');
      $("#storename").attr("data-search", "failed");
});

固定

您可能希望通過$('#storename').data('search')並通過$('#storename').data('search', 'failed') 混合HTML5數據屬性和jquery數據屬性似乎是個問題。

另外,請注意你所使用的雙等於== - 這里可能是無害的,但總是更好地使用嚴格(in)與===!=

長版

使用http://jsfiddle.net/gMam4/2/中的小提琴,看起來你正在設置並獲取'data- *'屬性。 問題是,這與$ el.data()調用沒有直接關系。 從jquery文檔:

數據屬性在第一次訪問數據屬性時被拉出,然后不再被訪問或變異(然后所有數據值都在內部存儲在jQuery中)。

長版本: http//api.jquery.com/data/#data-html5

因此,要么使用$ el.data('key',value)直接設置數據,要么使用$ el.val(value)。 在你的情況下,我會認為前者......

暫無
暫無

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

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