I have a web page with drop down menus and many other stuff.
I want to retrieve the value of dropdown menu, I know it can be done using $_POST after submission. But I want to retrieve it before submissions and hence using javascript code.
document.getElementById().value;
I am confused on how to write this code in my form page.
echo $form->dropDownlist($model2,'ename',CHtml::listData
(Lists::model()->findAll(),'ename','ename'));
How can I call the function to javascript in the form page?
The Dropdown id Like YourModelname_ename
Get the id of your dropdown and use this code
var dom = document.getElementById("idofyourdropdown");
var result= dom. options [dom. selectedIndex]. text; //give selected text
Related
I have one functionality to get the print of current page. So to prepare the content, I am showing everything on one popup up first then later a print popup and then print command.
Trying to do the same with below code.
$('#button').on('click',function() {
var popup = window.open("","mypopup","width=500,height=300");
var html = $("#form").html();
$(popup.document.body).html(html);
popup.print();
});
JSFiddle
Tried with clone() method as well but not working.
So the issue is, I am not getting the element's values in my popup.
Thanks
HI Use simple jquery line for each input field.
You can add a value attribute for your input fields.
I have changed your script a bit. Please refer it.
$('#button').on('click',function() {
var popup = window.open("","mypopup","width=500,height=300");
// I am setting the value attribute in the html . Do this for all fields
// with similar input fields you can have a jQuery('input').each(); loop
$('#name').attr('value',$('#name').val());
var html = $("#form").html();
$(popup.document.body).html(html);
popup.print();
});
Manually send the data from form through ajax call and store it in a local websql (browser)storage which can be retrived in the print preview dialog box
I'm changing the items in an ASP.NET DropDownList using jquery, but when I submit the form, the SelectedItem property of the DropDownList is null. Any idea how I can modify the list of items in the DropDownList with jquery without causing this error in the code behind?
Here's the way I'm setting up the DropDownList. Initially, it's an empty ASP.NET DropDownList.
var mailType = $("#ContentPlaceHolder1_ddlMailType");
mailType.empty();
mailType.append("<option></option>");
mailType.append("<option value='D'>Direct Mail</option>");
.
.
.
The DropDownList does end up showing the added items after the jquery executes.
Sorry, you cannot use jquery or Javascript to add items to a server-side ASP.NET web forms list control. The contents of the list control are stored in ViewState, and unless you've done something very strange, your jquery is not updating the ViewState. If you have validation enabled, ASP.NET will throw an error if the form post tag/value does not match any of the known values that are stored in ViewState.
If you wish to have listbox with items that are added dynamically on the client side, don't use a server side control. Just type the HTML into the ASP web form directly (don't use the runat=server tag). To read the contents of the control on postback, don't reference the control; instead, use HttpContext.Request.Form["tag name"]. Note that there is no way for your code behind to know what your jquery has added to the list box, as this information is not passed when the browser posts the form-- only the value attribute of the selected item is passed.
For some more related information, see one of my other answers here.
Couple of things you should check. 1) Modify your JavaScript to get the drop down client id dynamically. The client id changes with every post back event.
var mailType = $("<%= ddlMailType.ClientID %>");
mailType.empty();
mailType.append("<option></option>");
mailType.append("<option value='D'>Direct Mail</option>");
Also, 2) If you are binding the original values of the drop down list in code behind make sure you are handling the post back event. Otherwise the drop down will reset to its original value each time there is a post back.
If Not Page.IsPostBack Then
ddlMailType.databind()
End If
your control is declared with no items in the aspx. since nothing is added to the items collection that way and no adding to items is done in page_init or page_load (when IsPostback = false) when a postback occurs to check SelectedItem, the items property of your DropDownList is empty.
you can still get the value from the forms collection Request.Form[MyDDL.UniqueID]
more in depth here.
Why does DropDownList.SelectedValue is relied on viewstate?
I have form name as myForm with two elements dropdown and autocomplete() textbox. If an end-user selects the value on the dropdown and give a suggestions on the autocomplete (tokens) and click an apply button to get the search results matching our inputs.
My Screen Input : HTML Screen
My problem here is to retain the form values after submitting the form through jquery. My code is here:
$("#myForm").submit(function(){
$('#myForm').attr("method","post");
$('#myForm').attr('action','http://localhost:8080/LscaSearch/');
$('#myForm').submit();
});
I wanted to retain the form values after submit the form. Any help on this?
You can use asynchronous form submission using jQuery AJAX. That way you can show the results on the same page without having to refresh it.
I have a button in my php view by which I can add textboxes to my page inside a form. But Whe I am getting the value of that control in my php controller the value is not there.
Is there any way by which we can get the value of dynamically added textboxes inside a POST form.
if you know the number of textbox you have, add the textbox before with a css style display:none; and display it in js on click event - for exemple with jquery
$('.yourclass').show();
it will act like a classic form
if you don't know the number you can have
you can send the form by js and get the value by something like that:
var myArray = [];
$('.yourclass').each(function() {
//unidimentionnal array exemple
myArray.push($(this).attr('myAttribut'));
});
console.log(myArray);
Seb
I have one page(suppose first page) where I have a hidden Field control. Now from this page I need to go to another page(second page), but I am not using window.open method instead of that I am using document.form.submit() method. Now from second page I need to set the value of hidden Field which is on first page.
Because I am not using window.open() method so I can not use parent.window.opener for setting the value of hidden field.
So is there any way I can set the hidden field value from second page to first page.
(Again I am using document.form.submit() method from second page to go to first page).
Thanks.
You should use server side code to get the values submitted from previous page.
The form values automatically send the values in the form
OR
If you use get method, with the help of javascript you can get and assign the values
Check this
By Using SERVER Side Scripting Language like PHP, ASP, JSP etc.
Below is the example of PHP scripting language.
<form action="first.php"> ... </form>
In first.php:
<?php
echo $_GET['your input textbox name']; // if you define the **METHOD** as **GET** of form html element
or
echo $_POST['your input textbox name']; // if you define the **METHOD** as **POST** of form html element
or
even you can use like this: echo $_REQUEST['your input textbox name']; // it works for both **POST** or **GET**
?>
Form2 writes data to cookie or local db.
Start a timer on form1 after you open form2, check the data continuously.