最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript set input value when user click button - Stack Overflow

programmeradmin0浏览0评论

Im developing chrome extension. I want to change input value(username) to some text when user click button. But my solution not working. Need advise. My code is shown below:

<!DOCTYPE html>
<html>
<head>  
<META http-equiv=content-type content=text/html;charset=utf8>
<META http-equiv=content-type content=text/html;charset=windows-1254>
<META http-equiv=content-type content=text/html;charset=x-mac-turkish>
<script src="//ajax.googleapis/ajax/libs/jquery/1.10.2/jquery.min.js"></script>  
<title></title>
<script>
    $('#target').submit(function() {
    alert('something');
    return false;
    $('#UserName').value('test');
});
</script>
</head>
<body>
<form id="target">
<table>
  <tr>
    <td>UserName:</td>
    <td>
      <input type="text" id="UserName" size="20" />
    </td>
  </tr>
  <tr>
    <td>Password:</td>
    <td>
      <input type="password" id="Password" />
    </td>
  </tr>
  <tr>
    <td></td>
<td><input type="button" value="Login" id="Login" /></td>
  </tr>
</table>
</form>
</body>
</html>

Im developing chrome extension. I want to change input value(username) to some text when user click button. But my solution not working. Need advise. My code is shown below:

<!DOCTYPE html>
<html>
<head>  
<META http-equiv=content-type content=text/html;charset=utf8>
<META http-equiv=content-type content=text/html;charset=windows-1254>
<META http-equiv=content-type content=text/html;charset=x-mac-turkish>
<script src="//ajax.googleapis./ajax/libs/jquery/1.10.2/jquery.min.js"></script>  
<title></title>
<script>
    $('#target').submit(function() {
    alert('something');
    return false;
    $('#UserName').value('test');
});
</script>
</head>
<body>
<form id="target">
<table>
  <tr>
    <td>UserName:</td>
    <td>
      <input type="text" id="UserName" size="20" />
    </td>
  </tr>
  <tr>
    <td>Password:</td>
    <td>
      <input type="password" id="Password" />
    </td>
  </tr>
  <tr>
    <td></td>
<td><input type="button" value="Login" id="Login" /></td>
  </tr>
</table>
</form>
</body>
</html>
Share Improve this question asked Jul 18, 2013 at 13:46 ftdeveloperftdeveloper 1,0933 gold badges26 silver badges50 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2

Change your submit button from type button to submit from:

 <input type="button" value="Giriş" id="Login" /> 

to:

<input type="submit" value="Giriş" id="Login" />

Also change your JS code from:

$('#target').submit(function() {
alert('uyarı');
return false;
$('#UserName').value('deneme');

to:

$('#target').submit(function() {
 alert('uyarı');    
 $('#UserName').val('deneme');
 return false;

Your submit button needs to be of type 'submit'.

So this:

<input type="button" value="Login" id="Login" />

should be:

<input type="submit" value="Login" id="Login" />

Also, the appropriate jQuery function for getting/setting input values is .val().

Lastly, you don't want to return false until the end of the function.

$('#target').submit(function() {
    alert('something');      
    $('#UserName').val('test');
    return false;
});

See this working Fiddle.

This is working fine

$('input#Login').click(function() {        
    $('#UserName').val('test');
    return false;
});

jQuery uses the VAL rather than VALUE

in other words

<script>
    $('#target').submit(function() {
    alert('something');
    return false;
    $('#UserName').val('test');
});
</script>

and also the FORM is not submitado change the type of input and submit to the adciona FORM METHOD = "POST"

Guys thank you very much. I found. The problem is when you are developing google chrome application you must seperate javascript code from html. Otherwise javascript not work.

发布评论

评论列表(0)

  1. 暂无评论