简体   繁体   English

如何在 HTML 表单中将特定字符串添加到选择选项之前

[英]How to prepend a specific string to a select option in a HTML form

I have a very simple html form with a dropdown list with the name of images which I will be saving the selecting into the database:我有一个非常简单的 html 表单,其中包含一个带有图像名称的下拉列表,我会将选择的内容保存到数据库中:

<form name="htmlform" method="post" action="html_form_send.php">
    <table width="450px">
        <select name="rating">
            <option value="image1">image1</option>
            <option value="image2">image2</option>
            <option value="image3">image3</option>
            <option value="image4">image4</option>
        </select>
        <tr>
            <td colspan="2" style="text-align:center">
                <input type="submit" value="Submit">
            </td>
        </tr>
    </table>
</form>

What I want to do is to save the whole path of the image without showing the path to the user, so I would like to do is to append a static path to any of the selections of the user when they press submit eg prepend: “path/to/my/file” to the selected image so if they select image1 it would save path/to/my/file/image1.png into my database.我想要做的是保存图像的整个路径而不向用户显示路径,所以我想要做的是在用户按下提交时将静态路径附加到用户的任何选择,例如前置:“ path/to/my/file”到所选图像,因此如果他们选择 image1,它会将 path/to/my/file/image1.png 保存到我的数据库中。

Any idea how can I do this?知道我该怎么做吗?

You can do it by setting the path in the value attribute of each <option> .您可以通过在每个<option>value属性中设置路径来实现。 Either you do it server side or you can do it in jQuery :您可以在服务器端执行此操作,也可以在jQuery 中执行此操作:

$('[name="rating"] > option').each(function() {
    this.value = '/path/to/file/' + this.value;
})

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM