简体   繁体   English

如何从选定的下拉列表中将jquery值传递给Laravel 5.1中的变量?

[英]How can I pass a jquery value from a selected dropdown list to a variable in Laravel 5.1?

I have created field for (price) in the controller class like so. 我已经在控制器类中为(价格)创建了字段。

   protected $fields = [
        'tag' => '',
        'title' => '',
        'location' => '',
        'property_type' => '',
        'residence_name' => '',
        'bedroom_no' => '',
        'price' => '',
        'lease' => 0,
    ];

I have also created a column (price) in my table for migration like so. 我还为自己的迁移在表格中创建了一个列(价格)。

Schema::table('tags', function (Blueprint $table) {

    $table->string('location');
    $table->string('property_type');
    $table->string('residence_name');
    $table->string('bedroom_no');
    $table->string('price');
    $table->boolean('lease');

});

other fields are saved in the database because they are text input fields. 其他字段则保存在数据库中,因为它们是文本输入字段。 Here is a working sample code for location input field being saved in the database: 这是保存在数据库中的位置输入字段的工作示例代码:

<div class="form-group">
  <label for="location" class="col-md-3 control-label">
  Location / City
  </label>
   <div class="col-md-8">
     <input type="text" class="form-control" name="location" placeholder=""
       id="location" value="{{ $location }}">
 </div>
</div>

And here is the code for price dropdown, I want get the value of var price from jQuery and then pass it to the $price var in Laravel: 这是价格下降的代码,我想从jQuery获取var price的值,然后将其传递给Laravel中的$price var:

 <div class="form-group">
  <label for="price" class="col-md-3 control-label">
   Price
  </label>
  <div class="col-md-4">
   <select class="form-control" id="price" value="{{ $price }}">
    <option>10,000 - 15,000</option>
    <option>15,000 - 20,000</option>
    <option>20,000 - 25,000</option>
    <option>30,000 - 50,000</option>
    <option>50,000 and Above</option>
   </select>
  </div>
 </div>

with a jQuery script code to accept the input from user 与jQuery脚本代码一起接受用户的输入

<script>
$("#price").change(function () {
   var price = $(this).val();
   alert(price);
});
</script>

Add to your <select> tag a name attribute price and to every <option> tag attribute value with the value you desire. 在您的<select>标签中添加name属性price并在每个<option>标签属性value添加所需的值。

<select class="form-control" id="price" name="price">
    <option value="10,000 - 15,000">10,000 - 15,000</option>
    <option value="15,000 - 20,000">15,000 - 20,000</option>
    <option value="20,000 - 25,000">20,000 - 25,000</option>
    <option value="30,000 - 50,000">30,000 - 50,000</option>
    <option value="50,000 and Above">50,000 and Above</option>
</select>

You don't need anything else. 您不需要任何其他东西。 This is how HTML forms work. HTML表单就是这样工作的。

The value is attached to the name and you will be able to get it later with: 该值将附加在名称上,稍后您将可以通过以下方式获取它:

$request->input('price'); The argument you pass to $request->input is the name parameter of your input/select/checkbox/radio element in your form and it will be equal to whatever value was selected. 传递给$request->input的参数是表单中input / select / checkbox / radio元素的名称参数,它等于选择的任何value

Remove the javascript, because it's not needed in your case. 删除JavaScript,因为您的情况不需要它。

try this 尝试这个

<div class="form-group">
<label for="price" class="col-md-3 control-label">
   Price
  </label>
  <div class="col-md-4">
   <select class="form-control" id="price">
    <option value="10,000 - 15,000">10,000 - 15,000</option>
    <option value="15,000 - 20,000">15,000 - 20,000</option>
    <option value="20,000 - 25,000">20,000 - 25,000</option>
    <option value="30,000 - 50,000">30,000 - 50,000</option>
    <option value="50,000 and Above">50,000 and Above</option>
   </select>
  </div>
 </div>

And in jquery 而在jQuery中

$("#price").on("change", function () {
    var price = $(this).val(); 
    alert(price);
    //Here you can use ajax to call php function
    $.ajax({
        url : <your_function_path>,
        type : 'POST',
        data : {
            price : price
        },
        success : function(ret_data) {
        }
    });
});

And then in your php function get value through Request object with "input" or "get" function 然后在您的php函数中通过带有“ input”或“ get”函数的Request对象获取值

for eg. 例如 Suppose you have class called ABC and function called PQR 假设您有一个名为ABC的类和一个名为PQR的函数

class ABC extends Controller {
    public function PQR(Request $request) {
        $price = $request->get('price');
        ...
    }
}

You mention that you can save the text input fields in the database, so I suppose you know how to do form submissions in HTML, set up a post route in Laravel, capture the input in your controller, and then save to your model. 您提到可以将文本输入字段保存在数据库中,所以我想您知道如何使用HTML进行表单提交,在Laravel中设置发布路线,在控制器中捕获输入,然后保存到模型中。

If that's the case, and the only problem is the select dropdown, then it seems like you have forgotten the name attribute on the select tag and the value attributes on the option tags. 如果是这样,唯一的问题就是选择下拉列表,那么似乎您已经忘记了选择标签上的名称属性和选项标签上的值属性。

If you want to set a default value in the dropdown, like I guess you are trying to due with value={{ $price }} , then you should use the selected attribute instead. 如果您想在下拉列表中设置默认值,例如我想您要使用value={{ $price }} ,则应该使用selected属性。

<select class="form-control" id="price" name="price">
    <option value='10,000 - 15,000'>10,000 - 15,000</option>
    <option value='15,000 - 20,000'>15,000 - 20,000</option>
    <option value='20,000 - 25,000' selected='selected'>20,000 - 25,000</option>
    <option value='30,000 - 50,000'>30,000 - 50,000</option>
    <option value='50,000+'>50,000 and Above</option>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在jQuery插件中传递下拉选择的值 - how to pass dropdown selected value in jquery plugin 单击按钮提交后,如何将下拉列表中的选定值传递给url查询字符串? - How can I pass the selected values from the dropdown list to the url query string when button submit is clicked? 如何将来自jQuery变量的值传递给PHP? - How can I pass the value coming from a jQuery variable to PHP? 如何从主页访问局部视图下拉列表的选定值? - How can I access the selected value of my partial view dropdown list from the main page? 如何将所选值从下拉列表传递到下一页中的另一个下拉列表 - how to pass selected value from dropdown to another dropdown in next page 如何在 function 中使用从 function 之外的 select 下拉列表中选择的变量值并将其分配给 function 变量? - How can i use variable value inside function selected from select dropdown outside the function and assign it to a javascript variable? 如何捕获从下拉列表中选择的值? - How to capture value selected from a dropdown list? 从最后一页传递值并在下拉列表PHP上自动选择 - Pass A Value From Last Page And Auto Selected On The Dropdown List PHP 如何将下拉选择的值传递给另一个下拉菜单 - how to pass dropdown selected value to another dropdown 使用JavaScript从下拉列表L-5.1获取选择的值 - Get selected value from a dropdown L-5.1 using JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM