2017-04-24 99 views
0

我有下面的腳本,我想文本字段值傳遞給一個變量 並顯示在警報窗口如何將文本字段值傳遞給一個變量在Javascript

<form name='searchdata' id="search_form" > 
    <div class="col-xs-4" id="form_submit_ins"> 
    <label for="usr">Search Instance</label> 
    <input type="text" class="form-control" id="ins" id"searchval" placeholder="Instance Name"><br/> 
    <button class="btn btn-danger navbar-btn" onclick="form_submit();" >Search</button> 
    </div> 
    <div> 

    </div> 
    </form> 
    </div> 
    <script type="text/javascript"> 
     function form_submit() { 
     var item = document.getElementById('searchval').value; 
     alert(item); 

    } 
+1

我認爲你正在尋找.val()不是.value。編輯:這是jQuery,並沒有真正解決這個問題。 – Ken

+1

@Ken問題不是標記jQuery和'.VAL()'是不是對DOM的作品從'document.getElementById' ... –

+0

@RobM返回的節點的本地方法。你說得對,我今天遇到了很多問題,看起來我已經變得sl。。我的錯! – Ken

回答

2

你有一個錯字:

id"searchval" 

應該是:

id="searchval" 
+0

M非常感謝,我正在睡覺,並沒有意識到這一點。 –

+0

不用擔心,很高興幫幫忙:-) –

+0

你能不能幫我在這裏 –

0

ID應該等於searchval即id="searchval"

0

也有一些是不對您的HTML(它有兩個ID字段?是否缺少「=」號?

如果您修復HTML到您的代碼應工作:

<input type="text" class="form-control" id="searchval" placeholder="Instance Name"> 
0

input有兩個id的,它只能有一個id

變化:

<input type="text" class="form-control" id="ins" id"searchval" placeholder="Instance Name"> 

要:

<input type="text" class="form-control" id="searchval" placeholder="Instance Name"> 
相關問題