I want to code a line like this in my mvc view:
<input type="button" id="Show" name="Show" onclick="javascript: AjaxPostAndProcess('myController/MyAction',{input1: ($('#formId').find( 'input[name="input1"]').val()})" />
but the doublequotes around "input1" are inside the singlequotes enclosing the parm to .find, and it's all inside the doublequotes of the onclick expression. Is there any way to escape this to get it to work?
I would do it differently:
<input type="button" id="Show" name="Show" />
<script>
$('#Show').click(function () {
AjaxPostAndProcess('myController/MyAction', {
input1: $('#formId').find( 'input[name="input1"]').val()
});
}
</script>
Keeping it unobtrusive. But to answer you question you could replace them with "
Related
In my view page i had a button
<button id="submit_btn" onclick="submitData('{{$data}}')">Save Data</button>. If the $data contains special characters like ' or " the script will be resulted in an error. How can i handle this problem. Please help me.
You can escape quote characters with addslashes()
<button id="submit_btn" onclick="submitData('{{addslashes($data)}}')">Save Data</button>
Also see this question
You should try this :
<button id="submit_btn" onclick="submitData({{$data}})">Save Data</button>
I want to overwrite password pattern provided by foundation(zurb) but its not identifying my overwritten regular expression and generated error according to its by default expression.
By default,passwords validation provided by foundation is "must be at least 8 characters with 1 capital letter, 1 number, and one special character".
For that I have used following:
1)
<head>
<script>
$(document)
.foundation()
.foundation('abide', {
patterns: {
new: ^[a-zA-Z]\w{3,14}$,
}
});
</script></head>
<body>
<form class="custom" data-abide>
<div class="row">
<div class="large-12 columns">
<label>Password <small>required</small></label>
<input type="password" pattern="new" required >
<small class="error" data-error-message="">validation error.</small>
</div>
</div>
</form>
</body>
2)
<form class="custom" data-abide="">
</form>
3)Directly used pattern in input tag:
<input type="password" pattern="^[a-zA-Z]\w{3,14}$" required >
But all these are not working.Please suggest me something to resolve my problem.
You can find the sample program - here
Update the value before running foundation:
<script>
Foundation.libs.abide.settings.patterns.password = /^\w{3,14}$/;
$(document).foundation();
</script>
Replacing pattern called password does the trick. Change
<script>
$(document).foundation();
</script>
at the bottom of index.html to the following code:
<script>
$(document)
.foundation()
.foundation('abide', {
patterns: {
password: /^\w{3,14}$/,
}
});
</script>
Note that the regex you provided (^[a-zA-Z]\w{3,14}$) should be written as /^[a-zA-Z]\w{3,14}$/ in JavaScript.
I have the following code which gives me a syntax error - unterminated string constant.I've matched up the quotes can't seem to spot an issue. Any ideas? It works fine, the syntax error is just annoying.
<input type="button" class="my-button" value="" name="back" onclick="location.href='#Url.Action(Model.Back.Step.ToString(), "MyController")'" />
You can rewrite it like this:
<input type="button" class="my-button" value="" name="back"
onclick="#("location.href='"
+ Url.Action(Model.Back.Step.ToString(), "MyController")
+ "'")" />
Use an actionlink instead. This one creates a nice bootstrap button:
#Html.ActionLink("Cancel", "Index", "Home", null, new { #class = "btn btn-default" })
HTML
<form method="GET">
<input type="text" name="txt"/>
<input type="button" onclick="get()"/>
</form>
returns
Javascript
returns
function get(){
var ik=$('#txt').value();
$.get('action.php',{text:ik});
}
PHP
<?php $text=$_GET['txt']; print $text?>
When I look at firebug I get a notice saying that index txt is
undefined, I tried everything but it's not working. What am I doing
wrong?
$('#txt') will search for an element with id=txt and you don't have any element with id "txt" on your HTML code.
To get a value of the field you should use the .val() method.
The code that should work for you is:
$('input[name="txt"]').val();
Or define an id to your field and your code should be like this:
$('#txt').val();
In your PHP code your are looking for a variable called "txt" and in your Javascript code you are defining "text" as your variable so... you should read the $_GET['text'];
[]'s
Igor.
Where is your #txt element?
Try
<form method="GET">
<input type="text" id="txt" name="txt"/>
<input type="button" onclick="get()"/>
</form>
You are passing {text:ik}, so in your action.php you should get the value you want from $_GET['text'] not $_GET['txt']
Ok, so there are three problems going on.
First, you probably want id=txt in the HTML. You currently have name=txt. The #txt is going to retrieve the ID attribute in the HTML (not the Name attribute).
Here, use this for the HTML:
<form method="GET">
<!-- You need to have an ID value, if you are using #txt in jQuery -->
<!-- I assign both ID and Name to the same value -->
<input type="text" id="txt" name="txt"/>
<input type="button" onclick="get()"/>
</form>
Second, your call to action.php is passing the key "text", not "txt", and third you need to use val().
This might work for the JavaScript:
function get(){
// The #txt is getting an ID value
// You also need to use val() instead of value() ... according to #Igor
var ik=$('#txt').val();
// If PHP is looking for a txt key, you'll want that here as well
// The key could by "mykey" as long as PHP expects "mykey".
$.get('action.php',{txt:ik});
}
So, you'll want to correct both those errors.
The PHP looks fine.
Use the val() method instead off value();
HTML
<input type="button" id="go"/>
Script
$(function(){
var ik=$(input'[name=txt]').val();
$.get('action.php',{txt : ik},function(data){
//do whatever with the response from get call
});
});
I'm adding Internationalization to a tapestry app.
Is there a standard tapestry-3 technique to Internationalize strings that appear as Javascript literals?
For example:
<input jwcid="submitBtn" type="submit" accesskey="U" value="Update" class="actionBtn" onclick="return confirm('Are you sure that you want to do that?');"/></td>
Can I simply replace the question with a tapestry tag in this and any other context? Say something like:
<input jwcid="submitBtn" type="submit" accesskey="U" value="Update" class="actionBtn" onclick="return confirm('<span key="AreYouSure">Are you sure that you want to do that?</span>');"/></td>
This means that the source file contains an element inside an attribute which would be fine inside a JSP. Does tapestry-3 handle this? If not, is there a way to do this in tapestry-3?
This works fine in T3 as well - another option is to initialize your i18n js strings at the top of the page:
<script>
var jsStrings = {
sure : '<span key="AreYouSure"/>',
...
};
</script>
and then just use them:
<input jwcid="submitBtn" onclick="return confirm(jsStrings.sure);"/>